﻿using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Reflection;

namespace Ams.Extensions
{
    public static class PropertyInfoExtensions
    {
        public static TAttribute Attribute<TAttribute>(this MemberInfo prop)
        {
            var customAttributes = prop.GetCustomAttributes(true);
            var matches = customAttributes.OfType<TAttribute>();
            var attribute = matches.FirstOrDefault();
            return attribute;
        }

        public static string DisplayName(this PropertyInfo prop)
        {
            var displayNameAttr = prop.Attribute<DisplayAttribute>();
            if (displayNameAttr != null) return displayNameAttr.Name;

            var name = prop.Name.Titlize();
            return name;
        }
    }
}
